package com.lanou.zy.projecta.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lanou.zy.projecta.R;
import com.lanou.zy.projecta.util.OnItemClickListener;

import java.util.List;

/**
 * Created by dllo on 15/10/31.
 */
public class SearchRecyclerViewAdapter extends RecyclerView.Adapter<SearchRecyclerViewAdapter.SearchViewHolder> {
    private List<String> hotSearches;
    private OnItemClickListener itemClickListener;

    public void setOnItemClickListener(OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public void addData(List<String> datas) {
        hotSearches = datas;
        notifyDataSetChanged();
    }

    @Override
    public SearchViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View v = inflater.inflate(R.layout.search_recycler_view_item, null);
        return new SearchViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final SearchViewHolder holder, int position) {
        if (hotSearches != null && hotSearches.size() > 0) {
            String h = hotSearches.get(position);
            holder.searchTv.setText(h);
        }

        if (itemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getAdapterPosition();
                    itemClickListener.onItemClick(holder.itemView, position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return hotSearches != null && hotSearches.size() > 0 ? hotSearches.size() : 0;
    }

    class SearchViewHolder extends RecyclerView.ViewHolder {
        private TextView searchTv;

        public SearchViewHolder(View v) {
            super(v);
            searchTv = (TextView) v.findViewById(R.id.searchTv);
        }
    }
}
